<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

//年 、 月
$year = $_POST['year']?$_POST['year']:date("Y");
$month= $_POST['month']?(int)$_POST['month']:date("n");
$month= ($month<=9)?("0".trim($month)):$month;
//die();
//订单情况
$db->sql = "SELECT * FROM `orderdata2` WHERE YEAR(`pay_time`) = {$year} AND MONTH(`pay_time`) = {$month}";
$orders  = $db->doResultList2();
//用户列表
$db->sql = "SELECT * FROM admin WHERE id != 1 AND type=2 AND isActive = 1";
$admins  = $db->doResultList2();
$adminNum= count($admins);
//评分
foreach ($admins as $key=>$item){
	if (date("Ym")>=($year.$month)) {
		//echo 1777;
		$admins[$key]['pingfen'] = dataArray($db,$year,$month,$item['userName']);
		//var_dump($admins[$key]['pingfen']);
		if (date("Ym")>($year.$month)) {
			$getJiangJinArray['fenshus'][] = $admins[$key]['pingfen']['fenshu']['total'];
			//if($item['userName']==$_SESSION['userName']) $getJiangJinArray['fenshu']=$admins[$key]['pingfen']['fenshu']['total'];
		}
	}
}
//echo date("Ym")."--".$year.$month;
if (date("Ym")>($year.$month)) {
    $TotalBonus = 1440;
    $TotalPart = 0;
    for($_i=1;$_i<=$adminNum;$_i++){
        $TotalPart = $_i + $TotalPart;
    }
    for($_j=$adminNum;$_j>0;$_j--){
        $jiangJinArray[] = ((int)($TotalBonus/$TotalPart))*$_j;
    }
	// = array(576,432,288,144);
	foreach ($getJiangJinArray['fenshus'] as $key=>$item){
		$getJiangJin[] = getJiangJin($item, $getJiangJinArray['fenshus'], $jiangJinArray);
	}
	//var_dump($getJiangJin);
}else{
	echo 111;
}
//排名
function getJiangJin($searchFenshu,$FenShuArray,$jiangJinArray){
	rsort($FenShuArray);
	foreach ($FenShuArray as $key => $item){
		if ($item==$searchFenshu) {
			$b[]=$key;
		}
	}
	$c['sameNum'] = sizeof($b);
	$c['startNo'] = $b[0];
	for ($i=0;$i<$c['sameNum'];$i++){
		$c['jiangJins'][] = $jiangJinArray[$c['startNo']+$i];
	}
	$c['jiangJin'] = number_format(array_sum($c['jiangJins'])/$c['sameNum'],'2');
	return $c;
}
//dataArray();
function dataArray($db,$year,$month,$be_pingfener){
	//总的得分
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-') FROM pingfen WHERE pingfen_date>'{$year}-{$month}-01' AND pingfen_date<='{$year}-{$month}-31' AND be_pingfener = '{$be_pingfener}'";
	$array['fenshu']['total']=$db->doResultOne();
	//
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-') FROM pingfen WHERE pingfen_date>'{$year}-{$month}-07' AND pingfen_date<='{$year}-{$month}-14' AND be_pingfener = '{$be_pingfener}'";
	$array['fenshu']['week1']=$db->doResultOne();
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-') FROM pingfen WHERE pingfen_date>'{$year}-{$month}-14' AND pingfen_date<='{$year}-{$month}-21' AND be_pingfener = '{$be_pingfener}'";
	$array['fenshu']['week2']=$db->doResultOne();
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-') FROM pingfen WHERE pingfen_date>'{$year}-{$month}-21' AND pingfen_date<='{$year}-{$month}-28' AND be_pingfener = '{$be_pingfener}'";
	$array['fenshu']['week3']=$db->doResultOne();
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-') FROM pingfen WHERE pingfen_date>'{$year}-{$month}-28' AND pingfen_date<='{$year}-{$month}-31' AND be_pingfener = '{$be_pingfener}'";
	$array['fenshu']['week4']=$db->doResultOne();
	
	if (date("Y-m-d")>($year.'-'.$month.'-07') && date("Y-m-d")<=($year.'-'.$month.'-14')) {
		$array['isActive'] = 1;
		$dateArray = array($year.'-'.$month.'-07',$year.'-'.$month.'-14');
	}
	if (date("Y-m-d")>($year.'-'.$month.'-14') && date("Y-m-d")<=($year.'-'.$month.'-21')) {
		$array['isActive'] = 2;
		$dateArray = array($year.'-'.$month.'-14',$year.'-'.$month.'-21');
	}
	if (date("Y-m-d")>($year.'-'.$month.'-21') && date("Y-m-d")<=($year.'-'.$month.'-28')) {
		$array['isActive'] = 3;
		$dateArray = array($year.'-'.$month.'-21',$year.'-'.$month.'-28');
	}
	if ( date("Y-m-d")>($year.'-'.$month.'-28') && date("Y-m-d")<=($year.'-'.$month.'-31') ) {
		$array['isActive'] = 4;
		$dateArray = array($year.'-'.$month.'-28',$year.'-'.$month.'-31');
	}
	//登陆用户是否评论了该编辑
	$db->sql = "SELECT IFNULL(SUM(`fenshu`),'-1') FROM pingfen WHERE pingfen_date>'{$dateArray[0]}' AND pingfen_date<='{$dateArray[1]}' AND be_pingfener = '{$be_pingfener}' AND pingfener = '{$_SESSION['userName']}'";
	$array['isPingLunGaiBianJi'] = $db->doResultOne();
	//echo $array['isActive'];
	//登录用户的所有评分
	$db->sql = "SELECT fenshu FROM pingfen WHERE pingfen_date>'{$dateArray[0]}' AND pingfen_date<='{$dateArray[1]}' AND pingfener = '{$_SESSION['userName']}'";
	$array['allPingFen'] = $db->doResultList2();
	return $array;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" >
$(function($){
	$(".pingfen").click(function(){
		var username = $(this).data('username');
		var fenshu   = $(this).data('fenshu');
		if( isNaN(parseInt(fenshu)) ) fenshu=0;
		var td       = $(this).parents('td');
		var totalPinFen=$(this).parents("td").siblings('.totalPinFen');
		if($(this).siblings("select").val()){
			var newFenShu = $(this).siblings("select").val();
			
			//评分
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "username="+username+"&act=pingfen&fenshu="+newFenShu,
				dataType: "html",
				success: function(date){
					if(date==1){
						td.text(parseInt(fenshu)+parseInt(newFenShu));
						totalPinFen.text( parseInt(totalPinFen.data("total"))+parseInt(newFenShu) );
						$("option[value='"+newFenShu+"']").remove();
					}
				}
			});
		}else{
			alert("请输入有效分数！！");
		}
		
	});
});

</script>
<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->		
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">接下来你要做什么呢亲？</p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3>绩效列表</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
				我们将在每月7、14、21、28开启对应周期的评分，请大家互相评分，每个分数只能提交一次。您需要提交评分以后才能看到对方的得分。评分开启7天，逾期未评论，您的本周评分将为0。
					<form method="post" action="">						
						<select name="year">
						<?php for($i=2013;$i<=date(Y);$i++):?>
							<option value="<?php echo $i;?>" <?php echo ($i==$year)?"selected":"";?>><?php echo $i;?></option>							
						<?php endfor;?>					
						</select>年						
						<select name="month">
						<?php for($i=1;$i<=12;$i++):?>
							<option value="<?php echo ($i<10) ? "0{$i}" : $i;?>" <?php echo ($i==$month)?"selected":"";?>><?php echo ($i<10) ? "0{$i}" : $i;?></option>							
						<?php endfor;?>								
						</select>月
						<button class="button" type="submit" style="margin-left: 10px;">搜索</button>																		
					</form>
					
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
						<table>							
							<thead>
								<tr>
									<th style="width:;">姓名</th>									
									<th style="width:;">所属分类</th>									
									<th style="width:;">基准佣金</th>
									<th style="width:;">实际佣金</th>
									<th style="width:;">涨幅指数</th>
									<th style="width:;">第一周评分</th>
									<th style="width:;">第二周评分</th>
									<th style="width:;">第三周评分</th>
									<th style="width:;">第四周评分</th>
									<th style="width:;">总得分</th>
									<th style="width:;">得分奖金</th>
									<th>总奖励金额</th>									
								</tr>
							</thead>
							<tbody>
							<?php //if(count($orders) > 0):?>	
								<?php foreach($admins as $key => $val): //var_dump($val['pingfen']);?>
								<tr>
									<td><?php echo $val['nickName'];?></td>
									<td><?php echo $val['base_cate'];?></td>
									<td><?php echo $val['base_commission'];?></td>
									<td></td>
									<td></td>
									<?php for ($weekKey=1;$weekKey<=4;$weekKey++):?>
									<td>
									    <?php if ($_SESSION["type"]!=2):?>
									    <!-- 非选品身份 -->
									        <?php echo $val['pingfen']['fenshu']['week'.$weekKey];?>
									    <?php else:?>
									    <!-- 选品身份 -->
    										<?php if($val['pingfen']['isActive']==$weekKey && $val['pingfen']['isPingLunGaiBianJi']==-1 && $val['userName']!=$_SESSION['userName']):?>
    										<select><option value=""></option>
    											<?php for ($j=1;$j<$adminNum;$j++): $disable = FALSE; foreach ($val['pingfen']['allPingFen'] as $item2): if($item2['fenshu']==$j) $disable = TRUE;endforeach;?>
    											<option value="<?php echo $j?>" <?php if($disable):?>style="display: none;"<?php endif;?>><?php echo $j?></option>
    											<?php endfor;?></select><a href="###" class="pingfen" data-username="<?php echo $val['userName'];?>" data-fenshu="<?php echo $val['pingfen']['fenshu']['week'.$weekKey];?>">提交</>
    										<?php else: echo $val['pingfen']['fenshu']['week'.$weekKey];endif;?>
										<?php endif;?>
									</td>
									<?php endfor;?>
									
									<td class="totalPinFen" data-total="<?php echo $val['pingfen']['fenshu']['total'];?>"><?php if($val['pingfen']['isPingLunGaiBianJi']!=-1 || $val['userName']==$_SESSION['userName'] || date("Ym")>($year.$month)) echo $val['pingfen']['fenshu']['total'];?></td>
									<td><?php echo $getJiangJin[$key]['jiangJin'];?></td>
									<td><?php //echo $val['FSSort'];?></td>
								</tr>
								<?php endforeach;?>
								<tr>
									<td>合计</td>
									<td></td>
									<td>60000.00</td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
							<?php //else:?>						
								<!-- <tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="16" style="text-align: center;">没有该月绩效数据</td></tr>
							 --><?php //endif;?>
							</tbody>
						</table>
					</div>
					<!-- End #tab1 -->
				</div>
				<!-- End .content-box-content -->
			</div>
			<!-- End .content-box -->
			<div class="clear"></div>
			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->
		</div>
		<!-- End #main-content -->
	</div>
</body>
<!-- Download From www.exet.tk-->
</html>
